package org.akomantoso.core.data;

import java.io.IOException;

import java.util.ArrayList;
import java.util.List;

import org.akomantoso.core.data.attribute.Core;
import org.akomantoso.core.data.attribute.IdReq;
import org.akomantoso.core.data.attribute.LinkOpt;
import org.akomantoso.core.data.attribute.Outcome;
import org.akomantoso.core.data.attribute.Refers;
import org.akomantoso.core.io.Externalizable;
import org.akomantoso.core.io.Input;
import org.akomantoso.core.io.Output;
import org.akomantoso.core.io.OutputUtil;
import org.akomantoso.core.io.util.ParliamentaryAnalysisTypeElementUtil;

/**
 * The complex type parliamentaryAnalysisType lists all the properties associated to elements in the parliamentary
 * analysis.
 * 
 * <pre>
 * 	<xsd:complexType name="parliamentaryAnalysisType">
 * 		<xsd:choice minOccurs="1" maxOccurs="unbounded">
 * 			<xsd:element ref="quorum"/>
 * 			<xsd:element ref="count"/>
 * 		</xsd:choice>
 * 		<xsd:attributeGroup ref="core"/>
 * 		<xsd:attributeGroup ref="idreq"/>
 * 		<xsd:attributeGroup ref="outcome"/>
 * 		<xsd:attributeGroup ref="refers"/>
 * 		<xsd:attributeGroup ref="linkopt"/>
 * 	</xsd:complexType>
 * </pre>
 * 
 */
public class ParliamentaryAnalysisType implements Core, IdReq, Refers, LinkOpt, Outcome, Externalizable {

	// Mandatory
	private String _id;
	// Optional
	private String _evolvingId;
	// Optional
	private String _refersTo;
	// Optional
	private String _outcome;
	// Optional
	private String _href;

	// Mandatory (min 1).
	private final List<ParliamentaryAnalysisTypeElement> _elements = new ArrayList<ParliamentaryAnalysisTypeElement>(4);

	/**
	 * @see org.akomantoso.core.data.attribute.IdReq#getId()
	 */
	@Override
	public String getId() {
		return _id;
	}

	/**
	 * @see org.akomantoso.core.data.attribute.IdReq#setId(java.lang.String)
	 */
	@Override
	public void setId(String id) {
		_id = id;
	}

	/**
	 * @see org.akomantoso.core.data.attribute.IdReq#getEvolvingId()
	 */
	@Override
	public String getEvolvingId() {
		return _evolvingId;
	}

	/**
	 * @see org.akomantoso.core.data.attribute.IdReq#setEvolvingId(java.lang.String)
	 */
	@Override
	public void setEvolvingId(String evolvingId) {
		_evolvingId = evolvingId;
	}

	/**
	 * @see org.akomantoso.core.data.attribute.Outcome#getOutcome()
	 */
	@Override
	public String getOutcome() {
		return _outcome;
	}

	/**
	 * @see org.akomantoso.core.data.attribute.Outcome#setOutcome(java.lang.String)
	 */
	@Override
	public void setOutcome(String outcome) {
		_outcome = outcome;
	}

	/**
	 * @see org.akomantoso.core.data.attribute.LinkOpt#getHref()
	 */
	@Override
	public String getHref() {
		return _href;
	}

	/**
	 * @see org.akomantoso.core.data.attribute.LinkOpt#setHref(java.lang.String)
	 */
	@Override
	public void setHref(String href) {
		_href = href;
	}

	/**
	 * @see org.akomantoso.core.data.attribute.Refers#getRefersTo()
	 */
	@Override
	public String getRefersTo() {
		return _refersTo;
	}

	/**
	 * @see org.akomantoso.core.data.attribute.Refers#setRefersTo(java.lang.String)
	 */
	@Override
	public void setRefersTo(String refersTo) {
		_refersTo = refersTo;
	}

	public List<ParliamentaryAnalysisTypeElement> getElements() {
		return _elements;
	}

	/**
	 * @see org.akomantoso.core.io.Externalizable#writeExternal(org.akomantoso.core.io.Output)
	 */
	@Override
	public <T extends Output> void writeExternal(T out) throws IOException {
		OutputUtil.writeIdReq(out, this);
		OutputUtil.writeRefers(out, this);
		OutputUtil.writeOutcome(out, this);
		OutputUtil.writeLinkOpt(out, this);

		for (int i = 0, n = _elements.size(); i < n; i++) {
			_elements.get(i).writeExternal(out);
		}
	}

	/**
	 * @see org.akomantoso.core.io.Externalizable#readExternal(org.akomantoso.core.io.Input)
	 */
	@Override
	public <T extends Input> void readExternal(T in) throws IOException {
		_id = in.readAttribute(IdReq.ATTRIBUTE_ID);
		_evolvingId = in.readAttribute(IdReq.ATTRIBUTE_EVOLVING_ID);
		_href = in.readAttribute(LinkOpt.ATTRIBUTE);
		_outcome = in.readAttribute(Outcome.ATTRIBUTE);
		_refersTo = in.readAttribute(Refers.ATTRIBUTE);

		final String localName = in.getLocalName();
		in.readNextStartElement();
		ParliamentaryAnalysisTypeElementUtil.read(in, _elements, localName);
	}

}
